package com.jn.exam.question.service.impl;

import com.jn.exam.DateTimeUtil;
import com.jn.exam.HTMLUtil;
import com.jn.exam.question.entity.PersonalQuestion;
import com.jn.exam.question.mapper.PersonalQuestionMapper;
import com.jn.exam.question.mapper.PersonalQuestionStoreMapper;
import com.jn.exam.question.service.OssFeignService;
import com.jn.exam.question.service.PersonalQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 江南大学1033190417
 * @since 2022-01-18
 */
@Service
public class PersonalQuestionServiceImpl extends ServiceImpl<PersonalQuestionMapper, PersonalQuestion> implements PersonalQuestionService {

    @Autowired
    private OssFeignService ossFeignService;

    @Autowired
    private PersonalQuestionStoreMapper personalQuestionStoreMapper;

    @Override
    @Transactional
    public boolean removeQuestionByPersonalQuestionStoreId(String personalQuestionStoreId) {
        List<PersonalQuestion> personalQuestions = baseMapper.getQuestionByPersonalQuestionStoreId(personalQuestionStoreId);
        for (PersonalQuestion question : personalQuestions) {
            String name = question.getName();
            String answer = question.getAnswer();
            List<String> nameUrl = HTMLUtil.getUrl(name);
            List<String> answerUrl = HTMLUtil.getUrl(answer);
            for (String url : nameUrl) {
                boolean b = ossFeignService.deleteFileByUrl(url);
                if (!b) {
                    return false;
                }
            }
            for (String url : answerUrl) {
                boolean b = ossFeignService.deleteFileByUrl(url);
                if (!b) {
                    return false;
                }
            }
        }
        boolean b0 = baseMapper.removeQuestionByPersonalQuestionStoreId(personalQuestionStoreId);
        boolean b1 = personalQuestionStoreMapper.removeByPersonalQuestionStoreId(personalQuestionStoreId);
        if (b0 && b1) {
            return true;
        }
        return false;
    }

    @Async
    @Override
    public Future<Boolean> deletePersonalQuestionByIdComplete(String personalQuestionId) {
        PersonalQuestion personalQuestion = baseMapper.getPersonalQuestionById(personalQuestionId);
        String name = personalQuestion.getName();
        String answer = personalQuestion.getAnswer();
        List<String> nameUrl = HTMLUtil.getUrl(name);
        List<String> answerUrl = HTMLUtil.getUrl(answer);
        for (String url : nameUrl) {
            boolean b = ossFeignService.deleteFileByUrl(url);
            if (!b) {
                return new AsyncResult<>(false);
            }
        }
        for (String url : answerUrl) {
            boolean b = ossFeignService.deleteFileByUrl(url);
            if (!b) {
                return new AsyncResult<>(false);
            }
        }
        boolean b = baseMapper.deletePersonalQuestionByIdComplete(personalQuestionId);
        if (b) {
            return new AsyncResult<>(true);
        }
        return new AsyncResult<>(false);
    }

    @Override
    public boolean deletePersonalQuestionBysIdComplete(String[] personalQuestionIds) {
        return baseMapper.deletePersonalQuestionBysIdComplete(personalQuestionIds);
    }

    @Override
    public List<PersonalQuestion> getPersonalQuestionHasDeleted(String personalQuestionStoreId) {
        return baseMapper.getPersonalQuestionHasDeleted(personalQuestionStoreId);
    }

    @Override
    public boolean recoveryDeleted(String[] personalQuestionIds) {
        return baseMapper.recoveryDeleted(personalQuestionIds);
    }

    @Override
    public boolean deleteQuestionExpire(Date date) {
        return baseMapper.deleteQuestionExpire(date);
    }


    @Override
    public boolean removeByIds(String[] personalQuestionIds) {
        Map<String, Object> params = new HashMap<>();
        params.put("personalQuestionIds", personalQuestionIds);
        params.put("updateTime", new Date());
        return baseMapper.removeByIds(params);
    }
}
